package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mysql.jdbc.PreparedStatement;

public class Type {
	private static String driverName = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false";

	static {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static Map<String, List<String>> types;

	public static void main(String[] args) {
		types = getTypes();
		Connection con = getConnection();
		try {
			con.setAutoCommit(false);
			PreparedStatement ps = null;
			int i = 1;
			int level1 = 1;
			int level2 = 100;
			String sql = "insert into tb_type(type_id,type_name,type_level,type_parent) value(?,?,?,?)";
			ps = (PreparedStatement) con.prepareStatement(sql);
			for (String string : types.keySet()) {
				System.out.println(string);
				ps.setInt(1, level1);
				ps.setString(2, string);
				ps.setInt(3, 1);
				ps.setInt(4, 0);
//				ps.executeUpdate();
				ps.addBatch();
				for (int j = 0; j < types.get(string).size(); j++) {
					System.out.println("\t" + types.get(string).get(j));
//					String sql1 = "insert into tb_type(type_id,type_name,type_level,type_parent) value(?,?,?,?)";
//					ps = (PreparedStatement) con.prepareStatement(sql1);
					ps.setInt(1, level2++);
					ps.setString(2, types.get(string).get(j));
					ps.setInt(3, 2);
					ps.setInt(4, level1);
//					ps.executeUpdate();
					ps.addBatch();
				}
				level1++;
			}
			ps.executeBatch();
			con.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("done!");
	}

	public static Connection getConnection() {
		try {
			Class.forName(driverName);
			// return DriverManager.getConnection(url, user, password);
			return DriverManager.getConnection(url);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static Map<String, List<String>> getTypes() {
		// TODO Auto-generated method stub
		Map<String, List<String>> type = new HashMap<>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File("D:\\type.txt")));
			StringBuilder sb = new StringBuilder();
			String str = null;
			while ((str = br.readLine()) != null) {
				sb.append(str + "\n");
			}
			String s = sb.toString();
			String[] ss = s.split("\n\n");
			for (String string : ss) {
				String[] s1 = string.split("\n");
				List<String> list = new ArrayList<String>();
				for (int i = 0; i < s1.length; i++) {
					if (i == 0) {
						type.put(s1[0], list);
						continue;
					}
					list.add(s1[i]);
				}
			}
			type.size();
			System.out.println();
			type.keySet().toString();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return type;
	}
}
